package Dao.AdminDao;

import Entity.Admin;
import Entity.Course;
import Entity.Student;
import Entity.Teacher;
import Util.SQLUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
管理员后台 Dao层 负责SQL语句 与传入参数的传入
 */
public class AdminSQLDao {

    private SQLUtil sqlUtil = new SQLUtil();

    /*
    管理员登录 需要参数 管理员账号 管理员密码
     */
    public Admin selectAdmin(Admin admin){
        String adminId  = admin.getAdminId();
        String adminPassword = admin.getAdminPassword();

        Object object[] = new Object[]{adminId,adminPassword};

        String sql = "select *from adminInfo where adminId = ? and adminPassword = ?";

        ResultSet rs = sqlUtil.executeQuery(sql,object);  //execute() add delete update
                                                            //executeQuery()  select

        admin = null;
        try {
            if(rs.next()){

                adminId = rs.getString("adminId");
                adminPassword = rs.getString("adminPassword");

                admin = new Admin(adminId,adminPassword);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return admin;
    }


    /*
    改变课程状态 需要参数 课程Id 状态 0 未通过审核 1 通过审核
     */
    public int changeCourseStatus(Course course){
        String courseId = course.getCourseId();
        int status = course.getStatus();

        Object[] objects  = new  Object[]{status,courseId};

        String sql = "UPDATE courseInfo SET status = ? WHERE courseId = ?";

        return sqlUtil.execute(sql,objects);
    }


    /*
    查询所有学生 不需要参数 返回一个学生信息列表
     */
    public List<Student> queryAllStudent(){

        String sql = "select * FROM studentInfo";

        ResultSet rs = sqlUtil.executeQuery(sql,null);

        List<Student> list  = new ArrayList<>();

        try {

            while (rs.next()){
                String studentId =  rs.getString("studentId");
                String studentPassword = rs.getString("studentPassword");
                String studentName =  rs.getString("studentName");
                float totalCredits  = rs.getFloat("totalCredits");
                String studentMajor = rs.getString("studentMajor");
                String studentDesc  = rs.getString("studentDesc");

                Student stu = new Student();

                stu = new Student();
                stu.setStudentId(studentId);
                stu.setStudentPassword(studentPassword);
                stu.setStudentName(studentName);
                stu.setTotalCredits(totalCredits);
                stu.setStudentMajor(studentMajor);
                stu.setStudentDesc(studentDesc);

                list.add(stu);
            }

        } catch (SQLException e) {
                e.printStackTrace();
        }
        return list;

    }

    /*
    删除学生  需要参数 学生Id
     */
    public int adminDeleteStudent(Student student){
        Object object[] = new Object[]{student.getStudentId()};

        String sql = "DELETE FROM studentInfo WHERE studentId = ?";

        return sqlUtil.execute(sql,object);
    }


    /*
   查询所有教师 不需要参数 返回一个教师信息列表
    */
    public List<Teacher> queryAllTeacher(){

        String sql = "select * FROM teacherInfo";

        ResultSet rs = sqlUtil.executeQuery(sql,null);

        List<Teacher> list  = new ArrayList<>();

        try {

            while (rs.next()){
                String teacherId =  rs.getString("teacherId");
                String teacherPassword = rs.getString("teacherPassword");
                String teacherName =  rs.getString("teacherName");
                String teacherGrade =  rs.getString("teacherGrade");
                String teacherType =  rs.getString("teacherType");
                String department =  rs.getString("department");
                String teacherDesc =  rs.getString("teacherDesc");

                Teacher tea = new Teacher();
                tea.setTeacherId(teacherId);
                tea.setTeacherPassword(teacherPassword);
                tea.setTeacherName(teacherName);
                tea.setTeacherGrade(teacherGrade);
                tea.setTeacherType(teacherType);
                tea.setDepartment(department);
                tea.setTeacherDesc(teacherDesc);

                list.add(tea);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;

    }


    /*
   删除教师 需要参数 教师Id
  */
    public int adminDeleteTeacher(Teacher teacher){
        Object object[] = new Object[]{teacher.getTeacherId()};

        String sql = "DELETE FROM teacherInfo WHERE teacherId = ?";

        return sqlUtil.execute(sql,object);
    }



}
